package com.z.other.service.impl;

import com.z.base.Results;
import com.z.other.model.Files;
import com.z.other.mapper.FilesMapper;
import com.z.other.service.IFilesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.z.sys.model.User;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxy
 * @since 2022-03-07
 */
@Service
public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements IFilesService {

    @Resource
    private FilesMapper filesMapper;

    @Override
    public Results<Files> findPageByMap(Map<String, Object> map, Integer offset, Integer limit) {
        StringBuffer where = new StringBuffer("");
        if (map != null && map.size() > 0) {
            where.append("where ");
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String[] strArr = entry.getKey().split("_");
                where.append(strArr[0] + " ");
                if (strArr[1].equals("like")) {
                    where.append("like '%" + entry.getValue() + "%' and ");
                } else {
                    where.append("= '" + entry.getValue() + "' and ");
                }
            }
            where.delete(where.length() - 4, where.length());
        }
        return Results.success(filesMapper.conutByMap(where.toString()), filesMapper.findPageByWhereSql(where.toString(), offset, limit));

    }

    @Override
    public Files findById(String id) {
        if(StringUtils.isEmpty(id)){
            return new Files();
        }
        return filesMapper.selectById(Long.parseLong(id));
    }

}